﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Content;
using MethodWorx.Core.Linq;
using System.Data.Linq;

namespace MethodWorx.CMS.Linq
{
    public class LinqContentTemplateRepository : LinqRepository<LinqContentTemplate, ContentTemplate>, IContentTemplateRepository
    {
        private ContentTemplateRepository inner;

        public LinqContentTemplateRepository(DataContext context)
            : base(context)
        {
            this.inner = new ContentTemplateRepository(this.source);
            this.source = this.source.Where(s => s.Deleted == false);
        }

   
        public ContentTemplate FindByName(string name)
        {
            return inner.FindByName(name);
        }

        protected override void OnInsert(LinqContentTemplate entity)
        {
            this.PersistAggregate(entity);
            base.OnInsert(entity);
        }

        protected override void OnUpdate(LinqContentTemplate entity)
        {
            this.PersistAggregate(entity);
            base.OnUpdate(entity);
        }

        private void PersistAggregate(LinqContentTemplate entity)
        {
            foreach (var action in this.UpdateChildEntitySet(entity.ProxyProperties, entity.Properties,
                               (ptc) => ptc.ContentTemplateId = entity.Id).ToList())
            {
                /*if (action.Type == RepositoryActionType.Deleted)
                    entity.ProxyPageTemplateConstraints.Remove(action.Entity);
                else if(action.Type == RepositoryActionType.Inserted)
                    entity.ProxyPageTemplateConstraints.Add(action.Entity);*/
            }
        }
    }
}
